package com.adjy.pathbuilders.screens.content;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.adjy.pathbuilders.R;

public class CustomDialog {
	
	private static CustomDialog instance;
	
	private Dialog dialog;
	private Button confirmar, cancelar;
	
	private EditText editText;
	private TextView tvMens;
	
	public void setContext(Context context) {
		dialog = new Dialog(context);
		 
        dialog.setContentView(R.layout.custom_dialog);
        
        confirmar = (Button) dialog.findViewById(R.id.btn_Confirmar);
        cancelar = (Button) dialog.findViewById(R.id.btn_Cancelar);
        
        editText = (EditText) dialog.findViewById(R.id.etValor);
        tvMens = (TextView) dialog.findViewById(R.id.tvMens);
	}
	
	private CustomDialog() {}
	
	public synchronized static CustomDialog getInstance() {
		if(instance == null)
			instance = new CustomDialog();
		return instance;
	}
	
	public void createDialog(){
		dialog.show();
    }
	
	public void createDialog(String title, String requestInputMessage, String defaultInput, View.OnClickListener confirmOnClickListener, View.OnClickListener cancelOnClickListener){
		setTitle(title);
		setRequestInputMessage(requestInputMessage);
		setDefaultInput(defaultInput);
		setConfirmOnClickListener(confirmOnClickListener);
		setCancelOnClickListener(cancelOnClickListener);
		createDialog();
	}

	public void setTitle(String title) {
		dialog.setTitle(title);
	}
	
	public void setDefaultInput(String defaultInput) {
		editText.setText(defaultInput);
	}
	
	public void setRequestInputMessage(String requestInputMessage) {
		tvMens.setText(requestInputMessage);
	}
	
	public void setConfirmOnClickListener(View.OnClickListener confirmOnClickListener) {
		confirmar.setOnClickListener(confirmOnClickListener);
	}
	
	public void setCancelOnClickListener(View.OnClickListener cancelOnClickListener) {
		if(cancelOnClickListener == null){
			cancelOnClickListener = new View.OnClickListener() {
	            public void onClick(View v) {
	                dialog.dismiss(); // Ends the dialog
	            }
	        };
		}
		
		cancelar.setOnClickListener(cancelOnClickListener);
	}
	
	public Dialog getDialog() {
		return dialog;
	}
	
	public String getInput() {
		return editText.getText().toString();
	}
	
}
